package com.kx.web;

import com.kx.entity.Goods;
import com.kx.entity.GoodsCategory;
import com.kx.entity.ReceiveGoods;
import com.kx.service.GoodsCategoryService;
import com.kx.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * author: kxz
 * date  : 11/30
 */
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 查询所有商品并显示categorylevelone.name
     *
     * @param page
     * @param limit
     * @return DataVO<GoodsVO>
     */
    @RequestMapping(value = "/findGoods",method = RequestMethod.GET)
    @ResponseBody
    public Object findGoodsData(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        return goodsService.findData(page, limit);
    }

    /**
     * 根据id查询Good并显示categorylevelone.name
     *
     * @param gid
     * @return GoodVO
     */
    @RequestMapping("/findGoodVOById")
    @ResponseBody
    public Object findGoodVOById(Integer gid) {
        return goodsService.findGoodVOById(gid);
    }

    /**
     * @param gid
     * @return Goods
     */
    @RequestMapping(value = "/findGoodById",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Object findGoodById(Integer gid) {
        return goodsService.findGoodById(gid);
    }

    /**
     * 新增商品
     * 注意此处前端addGood传回后端的值中categorylevelone与Goods中categoryleveloneId不一致
     *
     * @param goods
     * @param categorylevelone
     * @return int
     */
    @PostMapping("/addGood")
    @ResponseBody
    public Object addGood(Goods goods, String categorylevelone) {
        goods.setCategoryleveloneId(Integer.valueOf(categorylevelone));
        return goodsService.addGood(goods);
    }

    /**
     * 用deleteById(Serializable id)操作显示delete from goods where null=?
     * 被迫暂时使用下方deleteGood
     *
     * @param gid
     * @return int
     */
    @DeleteMapping(value = "/deleteGoodById")
    @ResponseBody
    public Object deleteGoodById(Integer gid) {
        return goodsService.deleteGoodById(gid);
    }

    @RequestMapping(value = "/deleteGood",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteGood(Integer gid) {
        return goodsService.deleteGood(gid);
    }

    @RequestMapping(value = "/editGood",method = {RequestMethod.PUT,RequestMethod.POST})
    @ResponseBody
    public Object editGood(ReceiveGoods receiveGoods) {
        System.out.println("=============================="+receiveGoods);
        Goods goods=new Goods();
        BeanUtils.copyProperties(receiveGoods,goods);
        GoodsCategory goodsCategory=goodsCategoryService.findGoodCategoryByName(receiveGoods.getCategorylevelone());
        goods.setCategoryleveloneId(goodsCategory.getId());
        System.out.println("=============================="+goods);
        return goodsService.editGood2(goods);
    }

    /**
     * 查询所有商品种类以DataVO集合返回
     *
     * @return DataVO<GoodsCategory>
     */
    @RequestMapping("/findGoodsCategory")
    @ResponseBody
    public Object findGoodsCategory() {
        return goodsCategoryService.findGoodsCategory();
    }

    @RequestMapping("/findGoodsCategoryList")
    @ResponseBody
    public Object findGoodsCategoryList() {
        return goodsCategoryService.findGoodsCategoryList();
    }

    @RequestMapping("/getBarVO")
    @ResponseBody
    public Object getBarVO(){
        return goodsService.getBarVO();
    }

    @RequestMapping("/getPieVO")
    @ResponseBody
    public Object getPieVO(){
        return goodsService.getPieVO();
    }

//    @GetMapping("/{url}")
//    public Object redirect(@PathVariable("url") String url){
//        return url;
//    }

}
